﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BookFlightServices.Models;
using TongHopBookFlightDAO;
using TongHopBookFlightDAO.DAO;

namespace BookFlightServices.Controllers
{
    public class ChuyenBayController : ApiController
    {
        enum TypeFlight { MotChieu, KhuHoi };

        // GET api/chuyenbay
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/chuyenbay/5
        public MThongTinChuyenBay Get(string id)
        {
            MThongTinChuyenBay mThongTinChuyenBay = new MThongTinChuyenBay();

            ChuyenBay chuyenBay = ChuyenBayDAO.SelectFlightByFlightCode(id);

            List<ChuyenBay> dsChuyenBays = new List<ChuyenBay>();
            dsChuyenBays.Add(chuyenBay);

            mThongTinChuyenBay = ConverToThongTinChuyenBay(dsChuyenBays)[0];

            return mThongTinChuyenBay;
        }

        public MDoubleFlight Post([FromBody]SearchInfo value)
        {
            List<ChuyenBay> dsbaydi = ChuyenBayDAO.GetFlights(value.MaDiemDi, value.MaDiemDen, value.NgayDi);

            MDoubleFlight dbflight = new MDoubleFlight()
            {
                ChuyenBayDi = ConverToThongTinChuyenBay(dsbaydi),
                ChuyenBayVe = null
            };

            return dbflight;
        }

       
        public MDoubleFlight Put([FromBody]SearchInfo value)
        {
            List<ChuyenBay> dsbaydi = ChuyenBayDAO.GetFlights(value.MaDiemDi, value.MaDiemDen, value.NgayDi);

            List<ChuyenBay> dsbayve = ChuyenBayDAO.GetFlights(value.MaDiemDen, value.MaDiemDi, value.NgayVe);

            MDoubleFlight dbflight = new MDoubleFlight()
            {
                ChuyenBayDi = ConverToThongTinChuyenBay(dsbaydi),
                ChuyenBayVe = ConverToThongTinChuyenBay(dsbayve)
            };

            return dbflight;
        }

        private List<MThongTinChuyenBay> ConverToThongTinChuyenBay(List<ChuyenBay> dsChuyenBays)
        {
            List<MThongTinChuyenBay> lstResults = new List<MThongTinChuyenBay>();

            foreach (ChuyenBay chuyenBay in dsChuyenBays)
            {
                MThongTinChuyenBay mThongTinChuyenBay = new MThongTinChuyenBay();

                mThongTinChuyenBay.MaChuyenBay = chuyenBay.MaChuyenBay;
                mThongTinChuyenBay.TenHangHangKhong = HangHangKhongDAO.SelectByFlightID((int)chuyenBay.HangHangKhongID).TenHang;

                //Lấy danh sách chi tiết chuyến bay
                List<ChiTietChuyenBay> dsChiTietChuyenBays = ChiTietChuyenBayDAO.SelectByFlightID(chuyenBay.ChuyenBayID);
                mThongTinChuyenBay.dsChiTietChuyenBays = new List<MChiTietChuyenBay>();

                foreach (ChiTietChuyenBay chiTietChuyenBay in dsChiTietChuyenBays)
                {
                    MChiTietChuyenBay mChiTietChuyenBay = new MChiTietChuyenBay();
                    mChiTietChuyenBay.ThoiGianDi = chiTietChuyenBay.ThoiGianDi;
                    mChiTietChuyenBay.ThoiGianDen = chiTietChuyenBay.ThoiGianDen;
                    mChiTietChuyenBay.MaDiemDi = DiaDiemDAO.SelectByID((int)chiTietChuyenBay.DiemDiID).MaDiaDiem;
                    mChiTietChuyenBay.MaDiemDen = DiaDiemDAO.SelectByID((int)chiTietChuyenBay.DiemDenID).MaDiaDiem;

                    mThongTinChuyenBay.dsChiTietChuyenBays.Add(mChiTietChuyenBay);
                }


                //Lấy danh sách giá vé
                List<GiaVe> dsGiaVes = GiaVeDAO.SelectByFlightID(chuyenBay.ChuyenBayID);
                mThongTinChuyenBay.dsGiaVes = new List<MGiaVe>();
                foreach (GiaVe giaVe in dsGiaVes)
                {
                    MGiaVe mGiaVe = new MGiaVe();
                    mGiaVe.TenLoaiVe = LoaiGiaVeDAO.SelectByTicketTypeID((int)giaVe.LoaiGiaVeID).TenLoai;
                    mGiaVe.Gia = (double)giaVe.Gia;
                    mGiaVe.ChoNgoi = giaVe.ChoNgoi;

                    mThongTinChuyenBay.dsGiaVes.Add(mGiaVe);
                }

                //Lấy danh sách điểm dừng


                lstResults.Add(mThongTinChuyenBay);
            }
            return lstResults;
        }

       
        // DELETE api/chuyenbay/5
        public void Delete(int id)
        {
        }
    }
}
